//
//  SchoolSelectViewController.m
//  Comma
//
//  Created by 刘轩博 on 15/1/17.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CollegeRegisterViewController.h"
#import "AFNetworking.h"
#import "YRSideViewController.h"
#import "LoadIndicator.h"
#import "RegisterModel.h"
#import "PinYinForObjc.h"
#import "ChineseInclude.h"
@interface CollegeRegisterViewController ()
@property (nonatomic, strong)LoadIndicator *load;

@end

@implementation CollegeRegisterViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.load = [[LoadIndicator alloc] init];
        self.allCollegeArray = [NSMutableArray array];
        self.allCollegeDic = [NSMutableDictionary dictionary];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collegeSelected:) name:@"COLLEGESELECTED" object:nil];
        
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    // 不能划出侧边栏
    YRSideViewController *slide = [YRSideViewController share];
    slide.needSwipeShowMenu = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collegeView = [[CollegeRegisterView alloc] initWithExitImage:@"Button_close.pdf" collegeText:@"请输入大学名称"];
    
    // 设置collegeView为主视图
    self.view = self.collegeView;
    
    
    // 开始网络请求数据
    [self connectWithUrl];
    
    // 为退出按钮添加点击事件
    [self.collegeView.exitButton addTarget:self action:@selector(exitButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - 查找所有的大学数据
- (void)connectWithUrl
{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dic" ofType:@"plist"]];
    NSArray *keysArray = [dic allKeys];
    
   
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       
        for (int i = 0; i < [keysArray count]; i++) {
            
            NSString *chinese = [dic objectForKey:[keysArray objectAtIndex:i]];
            RegisterModel *model = [[RegisterModel alloc] init];
            model.name = chinese;
            //汉字
            [self.collegeView.tViewArray addObject:model];
            
            //全拼
            NSString *myKey = [NSString stringWithFormat:@"%d",i];
            [self.collegeView.schoolInfo setObject:[keysArray objectAtIndex:i] forKey:myKey];
            
            //转化汉字首字符的拼音
            NSString *tempPinYinHeadStr = [PinYinForObjc chineseConvertToPinYinHead:chinese];
            [self.collegeView.schoolHeadInfo setObject:tempPinYinHeadStr forKey:myKey];
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
           
            [self.collegeView.tableView reloadData];
            
        });

        
    });
        [self.collegeView createTableView];

}



#pragma mark -
- (void)collegeSelected:(NSNotification *)notify
{
    NSString *name = [notify.userInfo objectForKey:@"name"];
    [self.delegate assignCollegeName:name];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 返回点击事件
- (void)exitButtonAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 将所有大学转为拼音

- (void)changeChineseToPinYin
{
    for (NSDictionary *dic in self.allCollegeArray) {
        NSMutableString *name = [dic objectForKey:@"name"];
        NSString *pinYin = [name mutableCopy];
        CFStringTransform((__bridge CFMutableStringRef)pinYin, 0, kCFStringTransformMandarinLatin, NO);
        CFStringTransform((__bridge CFMutableStringRef)pinYin, 0, kCFStringTransformStripDiacritics, NO);
        pinYin = [pinYin stringByReplacingOccurrencesOfString:@" " withString:@""];
        [_allCollegeDic setObject:name forKey:pinYin];
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
